package com.example.demo.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Description 树
 * @Author wzh
 * @Date 2022/6/14 10:09 上午
 * @Version 1.0
 */
public class Lc98 {
    private static long pre = Long.MIN_VALUE;


    public static void main(String[] args) {
        //TreeNode treeNode1 = new TreeNode(1);
        //TreeNode treeNode3 = new TreeNode(3);
        //TreeNode treeNode6 = new TreeNode(6);
        //TreeNode treeNode4 = new TreeNode(4,treeNode3,treeNode6);
        //TreeNode treeNode5 = new TreeNode(5,treeNode1,treeNode4);

        //TreeNode treeNode1 = new TreeNode(0);
        //System.out.println(isValidBST(treeNode1));

        completableFutureAllOfTest();
    }


    static void completableFutureAllOfTest() {
        CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("Hello world1 start!");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Hello world1 wake!");
            return "Hello world1 finished!";
        });
        CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("Hello world2 start!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Hello world2 wake!");
            return "Hello world2 finished!";
        });
        List<CompletableFuture> completableFutureList = new ArrayList<>();
        completableFutureList.add(completableFuture1);
        completableFutureList.add(completableFuture2);
        CompletableFuture.allOf(completableFutureList.toArray(new CompletableFuture[0])).join();
    }




    public static boolean isValidBST1(TreeNode root) {
        return isValidBST1(root,Long.MIN_VALUE,Long.MAX_VALUE);


    }

    private static boolean isValidBST1(TreeNode root, long lower, long up) {
        if(root == null){
            return true;
        }
        if(root.val <= lower || root.val >= up){
            return false;
        }
        return isValidBST1(root.left, lower, root.val) && isValidBST1(root.right, root.val, up);
    }

    /**
     * 中序遍历
     * @param root
     * @return
     */
    public static boolean isValidBST(TreeNode root) {
        return inOrder(root);
    }

    private static boolean inOrder(TreeNode root) {
        if(root == null){
            return true;
        }
        boolean l = inOrder(root.left);
        if(root.val <= pre){
            return false;
        }
        pre = root.val;
        boolean r = inOrder(root.right);
        return l && r;
    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
